# @Time: 2020/08/20 16:21
# @Author: may
# @Desc:
import time

from selenium.webdriver.common.by import By
from logger import Logger

from data_entry import certificate


class ProjectApproval(certificate.Certificate):
    """立项批文"""

    logger = Logger('ProjectApproval').get_log()

    def click_project_approval(self):
        """点击立项批文按钮"""

        self.click_data_entry()
        self.select_credentials(3)

    def input_project_approval(self, date, id_, area, investment, current_area):
        """输入立项批文相关信息"""

        time.sleep(1)
        # 批文日期
        self.send_keys((By.NAME, 'Fzrq'), date)
        # 批文编号
        self.send_keys((By.NAME, 'Pwbh'), id_)
        # 总建筑面积
        self.send_keys((By.NAME, 'Jsgm_nr'), area)
        # 计划总投资
        self.send_keys((By.NAME, 'Jhztz'), investment)
        # 本期使用面积
        self.send_keys((By.NAME, 'Bqydmj'), current_area)
        info = [date, id_, area, investment, current_area]
        info.sort()

        return info

    def add_project_approval(self, date, id_, area, investment, current_area):
        """添加立项批文"""

        self.click_btn_of_certificate(3, self.add_btn)
        # 输入相关信息
        info_page = self.input_project_approval(date, id_, area, investment, current_area)
        # 保存
        self.click((By.XPATH, "//a[@class='saveBlue rightHandel']"))
        time.sleep(1)
        # 添加附件
        self.click((By.LINK_TEXT, '添加附件'))
        count = self.upload_easy()
        # 表数据
        info_table = self.get_project_approval(self.project_id, id_)

        msg = 'failure'
        if count and info_page == info_table:
            msg = 'success'
            self.logger.info('[立项批文]添加成功')
        else:
            self.logger.error('[立项批文]添加失败')
            self.logger.error(f'[立项批文]上传附件数量：{count}')
            self.logger.error(f'[立项批文]前台信息：{info_page}')
            self.logger.error(f'[立项批文]后台信息：{info_table}')

        return msg

    def update_project_approval(self, date, id_, area, investment, current_area):
        """修改立项批文"""

        update_loc = (By.XPATH, '//tr[1]//td[8]//div[1]//a[2]//span[1]')
        self.click_btn_of_certificate(3, update_loc)
        # 输入相关信息
        info_page = self.input_project_approval(date, id_, area, investment, current_area)
        # 保存
        self.click((By.XPATH, "//a[@class='saveBlue rightHandel']"))
        time.sleep(1)
        # 表数据
        info_table = self.get_project_approval(self.project_id, id_)

        msg = 'failure'
        if info_page == info_table:
            msg = 'success'
            self.logger.info('[立项批文]修改成功')
        else:
            self.logger.error('[立项批文]修改失败')
            self.logger.error(f'[立项批文]前台信息：{info_page}')
            self.logger.error(f'[立项批文]后台信息：{info_table}')

        return msg

    def detail_project_approval(self):
        """详情-立项批文"""

        detail_loc = (By.XPATH, '//tr[1]//td[8]//div[1]//a[1]//span[1]')
        self.click_btn_of_certificate(3, detail_loc)
        # 获取页面数据
        info = self.get_info_of_detail(stop=5)
        approval_id = info[1]
        # 获取db数据
        info_db = self.get_project_approval(self.project_id, approval_id)

        # 页面附件数量
        count_annex = self.get_count_files()
        certificate_type = 2
        sql = 'SELECT a.ZsGuid FROM djlxpw a WHERE a.Xmid = ? AND a.Pwbh = ?'
        # db附件数量
        count_annex_db = self.get_count_of_annex(certificate_type, self.project_id, approval_id, sql)
        # 检查前后台数据
        certificate_name = '立项批文'
        info.sort()
        msg = self.check_detail_data(certificate_name, info, info_db, count_annex, count_annex_db)
        return msg

    def delete_project_approval(self):
        """删除立项批文"""

        option = 3
        certificate_name = '立项批文'
        msg = super().delete(option, certificate_name)
        return msg

    def export(self, **kwargs):
        """导出"""

        return super().export(3, '立项批文')

    def get_project_approval(self, project_id, id_):
        """根据项目id以及立项批文id获取立项批文"""

        conn = self.get_conn()
        cursor = conn.cursor()
        with conn:
            cursor.execute(
                """
                SELECT t.Fzrq,
                       t.Pwbh,
                       convert(VARCHAR(18), convert(DECIMAL(18, 2), t.Jsgm_nr)),
                       convert(VARCHAR(18), convert(DECIMAL(18, 4), t.Jhztz)),
                       convert(VARCHAR(18), convert(DECIMAL(18, 2), t.Bqydmj))
                FROM djlxpw t
                WHERE t.Xmid = ?
                  AND t.Pwbh = ?                      
                """, project_id, id_)
            row = list(cursor.fetchone())
            row.sort()
        return row
